import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * properties 文件操作
 * @author 管雷鸣
 *
 */
public class Properties {
	public java.util.Properties properties;	//application.properties
	public String path = "";	//properties 文件所在路径
	
	/**
	 * 传入一个绝对路径，加载这个绝对路径的 properties 文件
	 * @param path 传入如  /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
	 */
	public void load(String absPath){
		this.path = absPath;
		properties = new java.util.Properties();
		try {
			FileInputStream in = new FileInputStream(path);
			properties.load(in);
			in.close();
		} catch (FileNotFoundException fineE) {
			System.out.println(absPath+"is not find. create...");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    
	public void save() {
		// 文件输出流
		File file = new File(path);
		if(!file.exists()) {
			//不存在，则创建
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			FileOutputStream fos = new FileOutputStream(path);
			// 将Properties集合保存到流中
			properties.store(fos, "shell auto update properties file : https://gitee.com/mail_osc/properties");
			fos.close();// 关闭流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 执行命令行如：
	 * java -cp properties-1.0.jar Properties -path=/root/test.properties -set database.ip=127.0.0.1
	 * @param args
	 */
	public static void main(String[] args){ 
//		这几行方便测试用的
//		args = new String[3];
//		args[0] = "-path=/Users/apple/Downloads/application.properties";
//		args[1] = "-set";
//		args[2] = "a=1?b=23";
		
		if(args.length == 0) {
			System.out.println("common operation:");
			System.out.println("-path\tset properties file absolute path. for example : -path=/root/test.properties");
			System.out.println("-set\tupdate properties file. if file not find, create. for example : -set database.ip=127.0.0.1");
			System.out.println("-get\tgain properties value. for example : -get database.ip");
			System.out.println("------  for example  -----");
			System.out.println("java -cp properties.jar Properties -path=/root/test.properties -set database.ip=127.0.0.1");
			return;
		}
		
		/*
		 * 命令
		 * -set 设置
		 * -get 读取
		 */
		String command = "";
		
		//操作的properties文件的路径
		String path = "";
		
		//命令后携带的参数，如 -set ，这里存储的是 database.ip=127.0.0.1 , 如 -get，这里存储的是database.ip 
		List<String> params = new ArrayList<String>();
		
		for (int i = 0; i<args.length; i++) {
//			System.out.println(args[i]);
			if(args[i].trim().equalsIgnoreCase("-set") || args[i].trim().equalsIgnoreCase("-get")) {
				//-set、 -get
				command = args[i].trim().toLowerCase();
			}else if(args[i].trim().indexOf("-path=") == 0){
				//设置path
				path = args[i].trim().split("=")[1];
			}else {
				//具体传入的参数
				params.add(args[i].trim());
			}
		}
		
		if(command.length() == 0) {
			System.out.println("error : java com.xnx3.PropertiesUtil please input : -get ? -set ?  for example : ");
			System.out.println("java com.xnx3.PropertiesUtil -set database.ip=127.0.0.1");
			return;
		}
		if(params.size() == 0) {
			System.out.println("error : java com.xnx3.PropertiesUtil please input params .  for example : ");
			System.out.println("java PropertiesUtil -set database.ip=127.0.0.1");
			return;
		}
		
		
		Properties util = new Properties();
		util.load(path);
		
		if(command.equals("-set")) {
			//设置
			for(int i = 0; i<params.size(); i++) {
				String param = params.get(i);
				if(param.indexOf("=") > 0) { //存在 = 号，那么输入合法，当然可能存在多个等号如 a=1?b=3 ，那么 1?b=3 是value
					int deng = params.get(i).indexOf("=");
					String key = param.substring(0,deng).trim();
					String value = param.substring(deng+1,param.length());
					System.out.println("set "+key+" = "+value);
					util.properties.setProperty(key, value);
				}
			}
			util.save();
		}else if (command.equals("-get")) {
			String key = params.get(0);
			Object obj = util.properties.get(key);
			if(obj != null) {
				System.out.print((String)obj);
			}
		}else{
			System.out.println("not find set ? get ? ");
			return;
		}
		
		
	}
}
